/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StartUpDialog.java
 *
 * Created on 02.06.2009, 15:20:49
 */
package peripheral.designer;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author Berni
 */
public class StartUpDialog extends javax.swing.JDialog {

    public enum StartOption {

        NEW,
        EXISTING
    }
    private static String[] allowedExtentions = new String[]{"zip"};
    JFileChooser fileChooser;
    File configurationFile;
    StartOption option;

    /** Creates new form StartUpDialog */
    public StartUpDialog() {
        super((java.awt.Frame)null, "PDC - Peripheral Display Creator", true);
        initComponents();

        this.setSize(550, 370);
        this.setLocation(
                getToolkit().getScreenSize().width / 2 - this.getWidth() / 2,
                getToolkit().getScreenSize().height / 2 - this.getHeight() / 2 - 50);
        this.setResizable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        createNewButton = new javax.swing.JButton();
        loadExistingButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setPreferredSize(new java.awt.Dimension(463, 60));

        createNewButton.setText("Create new...");
        createNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createNewButtonActionPerformed(evt);
            }
        });
        jPanel2.add(createNewButton);

        loadExistingButton.setText("Load existing...");
        loadExistingButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadExistingButtonActionPerformed(evt);
            }
        });
        jPanel2.add(loadExistingButton);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/peripheral/resources/pdc300x300.png"))); // NOI18N
        getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loadExistingButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadExistingButtonActionPerformed

        if (fileChooser == null) {
            fileChooser = new JFileChooser();
            fileChooser.setFileFilter(new ConfigFileFilter());
            fileChooser.setDialogTitle("Select an configuration file to load");
        }

        if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            this.configurationFile = fileChooser.getSelectedFile();

            option = StartOption.EXISTING;

            //close this dialog
            this.setVisible(false);
            this.dispose();
        }
    }//GEN-LAST:event_loadExistingButtonActionPerformed

    private void createNewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createNewButtonActionPerformed
        option = StartOption.NEW;

        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_createNewButtonActionPerformed

    public StartOption showStartUpDialog() {
        this.setVisible(true);

        return option;
    }

    public File getConfigFile() {
        return this.configurationFile;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton createNewButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton loadExistingButton;
    // End of variables declaration//GEN-END:variables

    class ConfigFileFilter extends FileFilter {

        //Accept all directories and all gif, jpg, tiff, or png files.
        public boolean accept(java.io.File f) {
            if (f.isDirectory()) {
                return true;
            }

            for (String support : allowedExtentions) {
                if (f.getName().toLowerCase().endsWith(support)) {
                    return true;
                }
            }

            return false;
        }

        //The description of this filter
        public String getDescription() {
            return "Configurationfiles";
        }
    }
}
